﻿@model cloudscribe.Core.Web.ViewModels.Account.EditUserViewModel
@using cloudscribe.Core.Web
@using Microsoft.Extensions.Options
@using Microsoft.Extensions.Localization
@using Microsoft.AspNetCore.Authorization
@inject IStringLocalizer<CloudscribeCore> sr
@inject IAuthorizationService _authorizationService
@{
    var userAuthResult       = await _authorizationService.AuthorizeAsync(User, PolicyConstants.UserManagementPolicy).ConfigureAwait(false);
    var roleLookupAuthResult = await _authorizationService.AuthorizeAsync(User, PolicyConstants.RoleLookupPolicy).ConfigureAwait(false);
    var canManageUserRoles   = userAuthResult.Succeeded && roleLookupAuthResult.Succeeded;
}
@if (Model.UserRoles.Count > 0)
{
    <h3>@sr["User Roles"]</h3>
    <ul class="list-unstyled">
        @foreach (var item in Model.UserRoles)
        {
            <li class="mb-2">
                <div class="row align-items-left pb-2 gx-0">
                    <div class="col-md-2 mt-1">
                        @item
                    </div>
                    <div class="col w-auto">
                        @if (canManageUserRoles)
                        {
                            <button type="button" class="btn btn-danger btn-xs" data-bs-toggle="modal" data-bs-target="@("#confirmRoleDeleteModal" + Model.UserRoles.IndexOf(item))">
                                @sr["Remove"]
                            </button>
                            <form asp-controller="UserAdmin" asp-action="RemoveRole">
                                <input name="roleName" value="@item" type="hidden" />

                        <input name="userId" value="@Model.UserId" type="hidden" />
                        <div class="modal fade" id="@("confirmRoleDeleteModal" + Model.UserRoles.IndexOf(item))" tabindex="-1" role="dialog" aria-labelledby="@("confirmRoleDeleteHeading" + Model.UserRoles.IndexOf(item))">
                            <div class="modal-dialog modal-sm" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h4 class="modal-title" id="@("confirmRoleDeleteHeading" + Model.UserRoles.IndexOf(item))">@sr["Confirm Delete"]</h4>
                                        <button type="button" data-bs-dismiss="modal" class="btn-close float-end" aria-label='@sr["Close"]'></button>
                                    </div>
                                    <div class="modal-body">
                                        @string.Format(sr["Are you sure you want to remove the role {0}?"], item)
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@sr["Cancel"]</button>
                                        <input type="submit" value='@sr["Remove"]' class="btn btn-danger btn-ok" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                }
                    </div>
                </div>
                
     
            </li>
        }
    </ul>
}

<div class="mb-3">
    <a id="AddToRolesModalLink" class="btn btn-xs btn-primary" asp-action="ModalAddUserToRoles" asp-controller="RoleAdmin"
       asp-route-siteId="@Model.SiteId"
       asp-route-userId="@Model.UserId"
       asp-route-pageSize="12"
       bs-modal-link="true">@sr["Add Roles"]</a>
</div>

